package org.linlinjava.litemall.db.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.linlinjava.litemall.db.dao.LitemallBrowseRecordMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.dto.AdminGoodsBrowserDTO;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class LitemallBrowseRecordService {
    @Resource
    private LitemallBrowseRecordMapper browseRecordMapper;

    public List<LitemallBrowseRecord> list(LambdaQueryWrapper<LitemallBrowseRecord> queryWrapper) {
        return browseRecordMapper.selectList(queryWrapper);
    }

    public List<LitemallBrowseRecord> querySelective(Integer goodsId, Integer categoryId, LocalDateTime startTime, LocalDateTime endTime,
                                                     Integer offset, Integer limit, String sort, String order,Integer shopId) {
        LitemallBrowseRecordExample example = new LitemallBrowseRecordExample();
        LitemallBrowseRecordExample.Criteria criteria1 = example.or();

        if (!StringUtils.isEmpty(goodsId)) {
            criteria1.andGoodsIdEqualTo(goodsId);
        }
        if (!StringUtils.isEmpty(categoryId)) {
            criteria1.andCategoryIdEqualTo(categoryId);
        }
        if (!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)) {
            criteria1.andAddTimeBetween(startTime, endTime);
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        if(shopId != null){
            criteria1.andShopIdEqualTo(shopId);
        }

        criteria1.andDeletedEqualTo(false);
//        PageHelper.startPage(offset, limit);

        return browseRecordMapper.selectList(Wrappers.lambdaQuery(LitemallBrowseRecord.class)
                .eq(!StringUtils.isEmpty(goodsId),LitemallBrowseRecord::getGoodsId,goodsId)
                .eq(!StringUtils.isEmpty(categoryId),LitemallBrowseRecord::getCategoryId,categoryId)
                .between(!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime),LitemallBrowseRecord::getAddTime,startTime,endTime)
                .eq(shopId != null,LitemallBrowseRecord::getShopId,shopId)
                .eq(LitemallBrowseRecord::getDeleted,0)
                .orderByDesc(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order),LitemallBrowseRecord::getAddTime)
        );

        //return browseRecordMapper.selectByExample(example);
    }

    public List<LitemallBrowseRecord> queryBrowseUserCount(LocalDateTime startTimes, LocalDateTime endTimes,Integer shopId) {
        LitemallBrowseRecordExample example = new LitemallBrowseRecordExample();
        LitemallBrowseRecordExample.Criteria criteria1 = example.or();

        criteria1.andAddTimeBetween(startTimes, endTimes);

        if(shopId != null){
            criteria1.andShopIdEqualTo(shopId);
        }else{
            criteria1.andShopIdIsNotNull();
        }
        criteria1.andDeletedEqualTo(false);
        return browseRecordMapper.selectByExample(example);
    }
    public LitemallBrowseRecord queryByGoodsId(Integer goodsId) {
        LitemallBrowseRecordExample example = new LitemallBrowseRecordExample();
        example.or().andGoodsIdEqualTo(goodsId).andDeletedEqualTo(false);
        return browseRecordMapper.selectOneByExample(example);
    }
    public int add(LitemallBrowseRecord record) {
        record.setAddTime(LocalDateTime.now());
        return browseRecordMapper.insertSelective(record);
    }
    public int updateById(LitemallBrowseRecord record) {
        record.setAddTime(LocalDateTime.now());
        return browseRecordMapper.updateByPrimaryKeySelective(record);
    }

    public List<LitemallBrowseRecord> getBrowseNum(Integer shopId,Integer goodsId,LocalDateTime startTime, LocalDateTime endTime) {
        return browseRecordMapper.selectList(Wrappers.lambdaQuery(LitemallBrowseRecord.class)
                .between(!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime),LitemallBrowseRecord::getAddTime,startTime,endTime)
                .eq(shopId!=null,LitemallBrowseRecord::getShopId,shopId)
                .eq(LitemallBrowseRecord::getGoodsId,goodsId));
    }

    public List goodsBrowser(LocalDateTime day) {
        return browseRecordMapper.goodsBrowser(day).stream()
                .sorted(Comparator.comparingInt(AdminGoodsBrowserDTO::getGoodsNum))
                .collect(Collectors.toList());
    }
}
